﻿#include <iostream>
#include <lpb_reflect/reflect/reflect.hpp>

int cFn(std::string t1,std::string t2) {
	std::cout << __FUNCTION__ << " t1:" << t1 << " t2:" << t2 << std::endl;
	return 20;
}

void reflect_rigister() {
	//注册构造函数
	//REFLECT_REGISTER_CTOR(goo, int&, std::string, int);
	reflect::registerCtor<goo(int&, std::string, int)>("goo");
	reflect::registerCtor<foo(std::string,std::string)>("foo");

	//注册成员函数
	//REFLECT_REGISTER_MEMFUN("goo::test", &goo::test);
	reflect::registerMemFn<&goo::test>("goo::test");
	reflect::registerMemFn<&goo::test2>("goo::test2");
	reflect::registerMemFn<&foo::test>("foo::test");

	//注册成员属性
	//REFLECT_REGISTER_MEMBER("goo::data", &goo::m_data);
	reflect::registerMember<&goo::m_data>("goo::data");
	reflect::registerMember<&goo::m_str>("goo::str");

	//注册全局函数
	reflect::registerCstyleFn< &cFn>("cFn");
}

int main() {
	///注册
	reflect_rigister();
	
	///调用1
	std::cout << "[1] constructor call\n";
	int t = 10;
	auto goo_obj = reflect::Create("goo", std::ref(t),"cplusplus",66);			//利用反射创建对象
	std::cout << "pass ref:" << t << std::endl;

	std::cout << "\n[2] property set/get\n";
	reflect::set_property("goo::data", goo_obj, 123);									//利用反射设置属性
	reflect::set_property("goo::str", goo_obj, "reflect");								//利用反射设置属性
	int prop = reflect::get_property("goo::data", goo_obj);							//利用反射获取属性
	std::string prop2 = reflect::get_property("goo::str", goo_obj);				//利用反射获取属性
	std::cout << "prop:" << prop << std::endl;
	std::cout << "prop2:" << prop2 << std::endl;

	std::cout << "\n[3] method call\n";
	int ret = reflect::Call("goo::test", goo_obj, "cplusplus", 20);					//利用反射调用函数
	std::cout << "goo::test ret:" << ret << std::endl;
	reflect::Call("goo::test2", goo_obj);															//利用反射调用函数

	int ret2 = reflect::Call_Ansi("cFn", "cpp", "reflect");
	std::cout << "cFn ret:" << ret2 << std::endl;
	getchar();
}